package com.zhz.lib.cms.manage.faq;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.BeanUtils;

import com.zhz.lib.cms.domain.faq.Faq;
import com.zhz.lib.cms.domain.type.Sortmap;
import com.zhz.lib.cms.manage.BaseActionWithApprove;
import com.zhz.lib.cms.utils.zhz.ZhzSecurityUtil;
import com.zhz.lib.cms.utils.zhz.ZhzSortMapUtil;
import com.zhz.lib.cms.vo.faq.FaqVO;
import com.zhz.project.common.util.alibaba.StringUtil;
import com.zhz.project.common.util.hibernate.BaseDAO;
import com.zhz.project.common.util.zhzutil.ZhzHttpRequestUtil;
import com.zhz.project.common.util.zhzutil.ZhzStringUtil;

public class faqAction extends BaseActionWithApprove {

    private static final int length     = 30;
    private static String    TABLE      = "Faq";
    private static String    KEY        = "faqID";
    private static String    LIST       = "faqlist";
    private static String    ObjectName = "faq";

    //问题分类系列的 
    private Integer          faqfatherid;

    /**
     * 不给参数，默认执行此处
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    @Override
    public ActionForward unspecified(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request, HttpServletResponse response)
                                                                                              throws Exception {
        return faqlist(mapping, form, request, response);
    }

    /**
     * 显示常见问题列表
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public ActionForward faqlist(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, HttpServletResponse response)
                                                                                          throws Exception {

        // 获取新闻类型
        String sort = ZhzHttpRequestUtil.getParameters(request, "sort");

        request.setAttribute("sort", sort);

        //上面是提取参数并输出到页面中
        //////////////////////////////////////////////////////////////////////////
        String whereStatement = BaseDAO.WHERE;
        StringBuffer fanyeHref = new StringBuffer();
        fanyeHref.append("faqManage.do?method=faqlist");

        // 根据新闻来源进行查询
        if (StringUtil.isNotBlank(sort)) {
            whereStatement += "and sort =" + sort;
            fanyeHref.append("&sort=");
            fanyeHref.append(sort);
        }

        List<Faq> faqlist = getListWithFanyeHref(request, TABLE, whereStatement
                                                                 + BaseDAO.WHERE_DELETE,
            " order by " + KEY + " DESC", LIST, fanyeHref);

        List<FaqVO> faqVOlist = new ArrayList<FaqVO>();
        if (faqlist != null && faqlist.size() > 0) {
            for (Faq faq : faqlist) {
                faqVOlist.add(changeToVo(faq, true));
            }
        }

        // 查询的结果集
        request.setAttribute(LIST, faqVOlist);
        return mapping.findForward("faq-list");
    }

    /**
     * 添加常见问题时，先执行此函数，取出问题分类列表，然后传递给jsp
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward faqadd(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) {

        request.setAttribute("faqsortlist", ZhzSortMapUtil
            .getChildSortMapList(faqfatherid, baseDao));

        return mapping.findForward("faq-add");

    }

    /**
     * 保存新添加的常见问题！！
     */
    public ActionForward faqsave(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, HttpServletResponse response) {
        // 获取问题
        String question = ZhzHttpRequestUtil.getParameters(request, "question");
        // 获取问题分类
        String sort = ZhzHttpRequestUtil.getParameters(request, "sort");
        // 获取答案
        String answer = ZhzHttpRequestUtil.getParameters(request, "faqcontent");

        Faq faq = new Faq();
        faq.setQuestion(question);
        faq.setSort(sort);
        faq.setAnsdate(new Date());
        faq.setAnswer(answer);
        faq.setAnsperson(ZhzSecurityUtil.getOperatorName());
        faq.setAproveState(0);
        faq.setAproveTime(null);
        faq.setEditor(ZhzSecurityUtil.getOperatorName());
        faq.setEditorTime(new Date());
        faq.setIfDelete(0);

        boolean save = baseDao.save(faq);

        if (save) {
            request.setAttribute("add", "add");
            request.setAttribute("addresult", "addresult");
        }

        return mapping.findForward("faq-result");
    }

    /**
     * 编辑常见问题
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward faqedit(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, HttpServletResponse response) {

        Faq faq = (Faq) getRecord(request, TABLE, KEY, ObjectName);

        request.setAttribute("sort", ZhzSortMapUtil.getSortmap(faq.getSort(), baseDao));

        request.setAttribute("faqsortlist", ZhzSortMapUtil
            .getChildSortMapList(faqfatherid, baseDao));

        return mapping.findForward("faqedit-form");
    }

    /**
     * 保存编辑后的常见问题
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     */
    public ActionForward faqeditsave(ActionMapping mapping, ActionForm form,
                                     HttpServletRequest request, HttpServletResponse response) {

        String faqID = ZhzHttpRequestUtil.getParameters(request, "faqID");
        // 获取问题
        String question = ZhzHttpRequestUtil.getParameters(request, "question");
        // 获取问题分类
        String sort = ZhzHttpRequestUtil.getParameters(request, "sort");
        // 获取答案
        String answer = ZhzHttpRequestUtil.getParameters(request, "faqcontent");

        Faq faq = (Faq) baseDao.findOneObject(TABLE, KEY, faqID);

        faq.setQuestion(question);
        faq.setSort(sort);
        faq.setAnsdate(new Date());
        faq.setAnswer(answer);
        faq.setEditor(ZhzSecurityUtil.getOperatorName());
        faq.setEditorTime(new Date());

        request.setAttribute("edit", true);
        boolean update = baseDao.update(faq);

        if (update) {
            request.setAttribute("editresult", "editresult");
            request.setAttribute("edit", "edit");
        }

        return mapping.findForward("faq-result");
    }

    /**
     * 通过ajax请求，获取书评列表
     */
    public ActionForward faqContent(ActionMapping mapping, ActionForm form,
                                    HttpServletRequest request, HttpServletResponse response)
                                                                                             throws Exception {
        Faq faq = (Faq) getRecord(request, TABLE, KEY, ObjectName);

        request.setAttribute("sort", ZhzSortMapUtil.getSortmap(faq.getSort(), baseDao));

        return mapping.findForward("faq-content");
    }

    /**
     * 删除常见问题
     */
    public ActionForward faqdel(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                                HttpServletResponse response) throws Exception {
        delete(request, TABLE, KEY);
        return mapping.findForward("faq-result");
    }

    /**
     * 对选择的常见问题进行审核
     */
    public ActionForward faqApprove(ActionMapping mapping, ActionForm form,
                                    HttpServletRequest request, HttpServletResponse response)
                                                                                             throws Exception {
        approveRecord(request, TABLE, KEY);

        return mapping.findForward("faq-result");
    }

    /**
     * 将news转换为页面需要的数据
     * 
     * @param news
     */
    private FaqVO changeToVo(Faq faq, boolean needCut) {
        FaqVO faqVO = new FaqVO();
        BeanUtils.copyProperties(faq, faqVO);
        Sortmap smp = (Sortmap) baseDao.findOneObject("Sortmap", "sortMapId", faq.getSort());
        if (smp != null)
            faqVO.setSortString(smp.getValue());
        if (needCut && StringUtil.isNotBlank(faqVO.getQuestion()))
            if (faqVO.getQuestion().getBytes().length > length)
                faqVO.setQuestion(ZhzStringUtil.subStringWithDot(faqVO.getQuestion(), length));
        return faqVO;
    }

    public void setFaqfatherid(Integer faqfatherid) {
        this.faqfatherid = faqfatherid;
    }
}